package com.hpsk.shopstart;

import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author huangwang2832511019
 */
@SpringBootApplication
@ComponentScan(basePackages = {"com.hpsk","com.hpsk.webSocket","com.hpsk.webSocket.configurer"})
@Slf4j
@MapperScan(value = {"com.hpsk.*.model.*.dao"})
public class ShopStartApplication extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(ShopStartApplication.class);
	}
	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(ShopStartApplication.class);
		Environment env = app.run(args).getEnvironment();
		app.setBannerMode(Banner.Mode.CONSOLE);
		logApplicationStartup(env);
	}
	protected static void logApplicationStartup(Environment env) {
		String protocol = "http";
		if (env.getProperty("server.ssl.key-store") != null) {
			protocol = "https";
		}
		String serverPort = env.getProperty("server.port");
		String contextPath = env.getProperty("server.servlet.context-path");
		if (StringUtils.isBlank(contextPath)) {
			contextPath = "/doc.html";
		} else {
			contextPath = contextPath + "/doc.html";
		}
		String hostAddress = "localhost";
		try {
			hostAddress = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			log.warn("The host name could not be determined, using `localhost` as fallback");
		}

		String xxlJobAddress = env.getProperty("xxl.job.admin.addresses");
		log.info("\n------------------------------\t应用程序“{}”\t------------------------------" +
						"\n接口文档访问 URL:" +
						"\n本地: \t{}://localhost:{}{}" +
						"\n外部: \t{}://{}:{}{}" +
						"\n配置文件: \t{} " +
				        "\nxxl-job 访问URL: {}"+
						"\n----------------------------------------------------------------------------------" +
						"\n应用程序启动成功>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",
				env.getProperty("spring.application.name"),
				protocol,
				serverPort,
				contextPath,
				protocol,
				hostAddress,
				serverPort,
				contextPath,
				env.getActiveProfiles(),
				xxlJobAddress
				);
	}
}
